﻿<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="utf-8" Async="true" EnableEventValidation="false" MasterPageFile="~/administration/master_popup.master" Inherits="ACM.cmsarea08.Administration.Core.OptPageBase" %>
<%@ MasterType VirtualPath="~/administration/master_popup.master" %>
<asp:Content ID="PageBody" Runat="server" ContentPlaceHolderID="MasterBody">
<div id="option_popup">
<div class="popup_msgs">
Anteprima Profilo Utente



<script language="c#" runat="server">


		 private System.Data.DataSet dsUser{
    		get {
    			System.Data.DataSet tmp = null;
   				if (ViewState[base.ID + "_dsUser"]!=null)
   					tmp = (System.Data.DataSet)ViewState[base.ID + "_dsUser"]
   						;
    			return tmp;
    		}
    		set {
    			ViewState[base.ID + "_dsUser"] = value;	
    		}
    	} 
 		private System.Data.DataSet dsDG{
    		get {
    			System.Data.DataSet tmp = null;
   				if (ViewState[base.ID + "_dsDG"]!=null)
   					tmp = (System.Data.DataSet)ViewState[base.ID + "_dsDG"]
   						;
    			return tmp;
    		}
    		set {
    			ViewState[base.ID + "_dsDG"] = value;	
    		}
    	} 
		
		private System.Data.DataSet dsRuoli{
    		get {
    			System.Data.DataSet tmp = null;
   				if (ViewState[base.ID + "_dsRuoli"]!=null)
   					tmp = (System.Data.DataSet)ViewState[base.ID + "_dsRuoli"]
   						;
    			return tmp;
    		}
    		set {
    			ViewState[base.ID + "_dsRuoli"] = value;	
    		}
    	} 
 		
		 private ACM.cmsarea08.Administration.Core.DBClass dclass = new ACM.cmsarea08.Administration.Core.DBClass();
		 private ACM.cmsarea08.Function.TextClass tClass = new ACM.cmsarea08.Function.TextClass();
		
		private string findid = string.Empty;
		private string finduser = string.Empty;	
		private int selectedrole{
    		get {
    			
   				if (ViewState[base.ID + "selectedrole"]!=null)
   					return Convert.ToInt32(ViewState[base.ID + "selectedrole"]);
   				else		
    				return -1;
    		}
    		set {
    			ViewState[base.ID + "selectedrole"] = value;	
    		}
    	} 
			
		
	
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			//base.OnInit(e);
		}

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		
	//		this.testLang.LanguageChanged += new itareanet.CMSAREA.ControlGallery.CambioHandler(this.LinguaCambia);
			
		}
		
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(Request.QueryString["findid"]!=null)
				findid = Request.QueryString["findid"].ToString();
			else
				findid = "";			
			if(Request.QueryString["finduser"]!=null)
				finduser = Request.QueryString["finduser"].ToString();
			else
				finduser="";
			//lista_ruoli.SelectedIndex = selectedrole;
			if (!(Page.IsPostBack)) {
				lista_utenti.Items.Clear();
				lista_ruoli.Items.Clear();
				templist.Items.Clear();	
				RiempiListBox();
			}
			
		}
		
		private void Assegna(Object o, EventArgs e){
			string checkquery = "select count(*) from {0}utentiruoli where username='{1}' and ruolo='{2}' ";
			string query = "insert into {0}utentiruoli set username='{1}', ruolo='{2}', enabled='1', data_ins=Now(), data_mod=Now() ";
			string valori = templist_values.Value;
			if(valori.StartsWith("~"))
				valori = valori.Substring(1);
			string depe = "";
			if(lista_ruoli.SelectedValue.Trim()!="")
				depe = lista_ruoli.SelectedValue.ToString().Replace(lista_ruoli.SelectedItem.Text,"").Replace("<br>",",");
			string ruolo = lista_ruoli.SelectedItem.Text;
			if(setdep.Checked){
				ruolo += depe;
			}
			setdep.Checked = false;
		
			string[] utenti = valori.Split('~');
			string[] ruoli = ruolo.Split(',');
			
			for(int i=0;i<utenti.Length;i++){
			
				for(int p=0;p<ruoli.Length;p++){
					if(dclass.ExecuteRealScalar(string.Format(checkquery,dclass.TablePrefix,utenti[i].Trim(),ruoli[p].Trim()))<1)
						
					{
						//if(
						dclass.ExecuteScalar(string.Format(query,dclass.TablePrefix,utenti[i].Trim(),ruoli[p].Trim())); //)>0)
							//Response.Write("fatto!<hr>");
//						else
	//						Response.Write("fallito!<hr>");
					}
				}
			}
			templist.Items.Clear();
			dsDG = null;
			templist_values.Value="";
			RiempiListBox();
			
		}
		private void AcquireContent() {
			//config=(itareanet.CMSAREA.ControlGallery.NewsConfig)System.Configuration.ConfigurationSettings.GetConfig("ItanControls/NewsControl"); 
					    	
				string querydg = "select username,ruolo from " + dclass.TablePrefix + "utentiruoli as ruoli " +
						"order by username; ";
				if (dsUser==null) {
					dsUser = dclass.ExecuteDataset("select username,id from "+dclass.TablePrefix+"utenti order by username");
				}
				if (dsUser==null || dsUser.Tables.Count==0)
					return;
		//		dsUserView = tClass.DecodeSystem.Data.DataSet(dsUser);	
				
				
				if (dsRuoli==null) {
					dsRuoli = dclass.ExecuteDataset("select ruolo,dipendenze from "+dclass.TablePrefix+"ruoli order by ruolo");
				}
				if (dsRuoli==null || dsRuoli.Tables.Count==0)
					return;
				if (dsDG==null) {
				
					dsDG = dclass.ExecuteDataset(querydg);
				}
				if (dsDG==null || dsDG.Tables.Count==0)
					dgAss.Visible=false;
				else{
					dgAss.Visible=true;
					dgAss.DataSource = dsDG.Tables[0].DefaultView;
					dgAss.DataBind();
			}
			//	dsUserView2 = tClass.DecodeSystem.Data.DataSet(dsRuoli);			
			//	dsUser = tClass.DecodeSystem.Data.DataSet(dsUser);				
			//	PrepareContent();
				
		}
	
		
	
		private void RiempiListBox() {
					
			
			//if(dsUserView.Tables[0].Rows.Count==0)
			//MenuGrid.DataSource=null;
				dgAss.DataSource=null;
				if (dsUser == null || dsRuoli == null)
					AcquireContent();
				
				string codici = "";
				lista_utenti.Items.Clear();
				lista_ruoli.Items.Clear();
				int index = 0;
				int contoap = 0;
				int contsost = 0;
			//	lista_ruoli.Attributes.Add("onchange","javascript:VisualizzaDipendenze()");
				lista_utenti.Attributes.Add("onclick","javascript:PrendiSelezioneUtente()");
				lista_ruoli.Attributes.Add("onclick","javascript:PrendiSelezioneGruppi()");
			//	ArrayList items = new ArrayList();
				
				foreach (System.Data.DataRow dr in dsUser.Tables[0].Rows){
					string testo = "";
			
				
					string teston  = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(dr["username"].ToString()));// + " (" + imeil + ")");
				
					ListItem li = new ListItem(teston,HttpUtility.HtmlDecode(dr["username"].ToString()));
					//Response.Write(finduser + " " + dr["id"].ToString() + "<br>");
					if(dr["id"].ToString().Trim()==finduser.Trim()){
						li.Selected=true;
						templist_values.Value = dr["username"].ToString();
					}
					lista_utenti.Items.Add(li);
			
				}
				
			
				
				//lista_ruoli.Items.Add(new ListItem("-- Nessun Gruppo --","--"));
				
				if(dsRuoli != null){
			
				
				foreach (System.Data.DataRow dr in dsRuoli.Tables[0].Rows){

					string testo = HttpUtility.HtmlDecode(dr["ruolo"].ToString());
					string depe = dr["ruolo"].ToString() + "," + dr["dipendenze"].ToString().Trim().Replace(",","<br>");
					ListItem li = new ListItem(testo,depe);
				//	li.Attributes.Add("rel",dr["dipendenze"].ToString().Trim()==""?"\"nessuna dipendenza\"":dr["dipendenze"].ToString().Replace(",","<br/>"));
					lista_ruoli.Items.Add(li);
					if(dr["ruolo"].ToString()==findid)
						li.Selected = true;
					
					}
					
				}
				
		
		}
		
		
		
	
		
		
	
</script>

<div style="display:block;">
<div style="float:left;">
   
		<span>Utenti</span><br />
		<asp:ListBox ID="lista_utenti" Rows="32" runat="server" Width="300"></asp:ListBox><br />
		
	</div>	
		<div style="float:right;">
			
			

			Utenti da importare<br /><asp:ListBox ID="templist" SelectionMode="Multiple" Rows="20" Width="200" runat="server"></asp:ListBox>
			<br /><input type="button" ID="select_rem" Value="Rimuovi" onclick="javascript:RemoveItem()" runat="server"/>
			<br /><input type="button" ID="rem_all" Value="Rimuovi Tutti" onclick="javascript:SvuotaTemp()" runat="server"/><br />
			<br /><span>Ruoli</span><br /><asp:DropDownList ID="lista_ruoli" runat="server" EnableViewState="true"></asp:DropDownList>
			<br /><asp:CheckBox id="setdep" Text="Assegna dipendenze" runat="server"></asp:CheckBox><br />
			<div id="dipendenze" style="overflow:auto; height:46px; font-style:italic; border:thin solid #666666;" runat="server" title="Dipendenze"></div>
			<br /><asp:button Text="Assegna" ID="assegna" OnClick="Assegna" runat="server"/><br />
			
		
		
      	<input type="hidden" id="templist_values" value="" runat="server" />
		
      
		
</div>
<ptl:Itan_DataGrid id="dgAss" style="float:left;" runat="Server" Title="Assegnazioni" ShowTitle="true" autogeneratecolumns="false" ExtendedPager="false" PageNum="1" CurrentPage="1" SelectColumn="false" SelectColumnField="username" CheckingColumn="false" EOrdinabile="false" CssClass=""  JavascriptSort="True"  >
<alternatingitemstyle BackColor="#e1e1e1"></alternatingitemstyle>
<columns>
	<asp:TemplateColumn>
        <headertemplate>Utente</headertemplate>
    	<itemstyle Width="50px"></itemstyle>
        <itemtemplate>
        	<%# Eval("username") %>
        </itemtemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
        <headertemplate>Ruolo</headertemplate>
        <itemstyle Width="50px"></itemstyle>
        <itemtemplate>
        	<%# Eval("ruolo") %>
        </itemtemplate>
    </asp:TemplateColumn>
	<asp:TemplateColumn>
        <headertemplate></headertemplate>
    	<itemstyle Width="10px"></itemstyle>
        <itemtemplate>
			<a href="#" onclick="Elimina('<%# Eval("username").ToString() + "|" + Eval("ruolo").ToString()%>')" >elimina</a>
            
        </itemtemplate>
    </asp:TemplateColumn>           
	<asp:BoundColumn DataField="username" Visible="false"></asp:BoundColumn> 
</columns>

</ptl:itan_datagrid>

</div>
</div></div>

<script language="javascript" type="text/javascript">
//<![CDATA[

var risultato = false;
		

var codiceAlice = '';
var codiceProvedi = '';
var listaimport = '';
var indiceAlice = 0;
var indiceProvedi = 0;
var listalice = document.getElementById('<% Response.Write(lista_utenti.ClientID); %>');
var listprovedi = document.getElementById('<% Response.Write(lista_ruoli.ClientID); %>');
var listemp = document.getElementById('<% Response.Write(templist.ClientID); %>');
var tempvalues = document.getElementById('<% Response.Write(templist_values.ClientID); %>');
document.getElementById('<% Response.Write(assegna.ClientID); %>').disabled = true;


 
function init(){
	if(tempvalues.value!="")
		PrendiSelezioneUtente();
	//listalice.selectedIndex=-1;
	VisualizzaDipendenze();
}
function Elimina(id){
	
	var parametri = id.split('|');
	XmlReader.send('/administration/Core/xml_server.asmx/EliminaRuoloUser','user='+parametri[0]+'&ruolo='+parametri[1]);
}
function SvuotaTemp() {
	
	if(listemp.options.length > 0)
		tempvalues.value = '';
	while (listemp.options.length > 0) {
    	listemp.options[0] = null;
		
	}
	
	document.getElementById('<% Response.Write(assegna.ClientID); %>').disabled = true;
}
function RemoveItem(){

	for (i=listemp.options.length-1;i>-1;i--){
		if (listemp.options[i].selected == true){
		//	var i = listemp.options.length;
			if(tempvalues.value.indexOf('~',tempvalues.value.indexOf('~'+listemp.options[i].value)+1)==-1){
				tempvalues.value=tempvalues.value.replace('~' + listemp.options[i].value + '~','~');
			}
			else{
				tempvalues.value=tempvalues.value.replace('~' + listemp.options[i].value + '~','~');
			}
			listemp.options[i] = null;
		}
	}
	if(listemp.options.length == 0)
		document.getElementById('<% Response.Write(assegna.ClientID); %>').disabled = true;
}



	
		

//function PrendiSelezioneAlice(codice,indice){
function PrendiSelezioneUtente(){
	
	var indice = listalice.selectedIndex;
	/*alert(indice);
	for(var p = 0;p<listalice.options.length;p++){
		if(listalice.options[p].selected==true)
			indice = p;
	}
	*/
//	alert(indice);
	var o = document.createElement("option");
	o.text = listalice.options[indice].text;
	o.value = listalice.options[indice].value;
	var i = 0;
	var r = false;
	for(i=0;i<listemp.length;i++){
		if(o.text == listemp.options[i].text)
			r=true;
	
	}
	if(!r){
		listemp.options[listemp.length] = (o);
		tempvalues.value += '~' + o.value;
		document.getElementById('<% Response.Write(assegna.ClientID); %>').disabled = false;
	}
//	listemp.add(new Option(o.text,codice));
//	alert('sono qui : ' + listalice.selectedIndex);
	codiceAlice = listalice.options[indice].value;
	indiceAlice = indice;
}



//function PrendiSelezioneProvedi(cod,indice){
function PrendiSelezioneGruppi(){
//	alert(cod);
	var cod = listprovedi.options[indiceProvedi].text;
	var indice = listprovedi.selectedIndex;
	codiceProvedi = cod;
	indiceProvedi = indice;
	VisualizzaDipendenze();
	
}
function VisualizzaDipendenze(){
	//alert("#"+listprovedi.options[indiceProvedi].getAttribute("rel")+"#");
	
	var div = document.getElementById('<% Response.Write(dipendenze.ClientID); %>');
	var	setdep = document.getElementById('<% Response.Write(setdep.ClientID); %>');
	var dip = listprovedi.options[listprovedi.selectedIndex].value;
	if(dip==listprovedi.options[listprovedi.selectedIndex].text + ','){
		div.innerHTML = "\"Nessuna dipendenza\"";
		setdep.disabled = 'disabled';
		return;
	}
	var indice = listprovedi.selectedIndex;
	
	setdep.disabled = '';
	div.innerHTML = dip.replace(listprovedi.options[listprovedi.selectedIndex].text + ',','');
	
}

init();
var XmlReader = new XHR({method: 'post', 
					onSuccess: function(result) {
//						alert(result);
						if(result!=null) {
							history.go(0);	
							window.location.reload();	
														
						}
						init();
					 }
		 });


//]]>
</script>
</asp:Content>